/// beamnpc.txt
// A monster with a special ability scripted in. The ability is an beam, used 
// 50% of the time.

// Memory Cells:
//   Cell 0 - If 0, default behavior. If 1, it doesnt fidget.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.
//   Cell 4 - The ability to use.
//   Cell 5 - talking

// The abilities:
// 0 - Beam Slow
// 1 - Beam Curse
// 2 - Beam Fire (1-6 dam per level)
// 3 - Beam Acid
// 4 - Beam Terror
// 5 - Beam Daze
// 6 - Beam Debuff
// 7 - Beam Cold (1-8 dam per level)
// 8 - Beam Energy (1-10 dam per level)
// 9 - Beam Charm
// 10 - unused
// 11 - Beam lightning

begincreaturescript;

variables;

short i,target;
short last_abil;
short used_abil = 0;
short mental_effect = 0;
short mess1 = 0;
short mess2 = 0;
short mess3 = 0;

body;

beginstate INIT_STATE;
	last_abil = get_current_tick();

	change_max_health(ME,30);
	set_boss_level(ME,2);
	
	set_resistance(ME,0,98);
	set_resistance(ME,1,98);
	set_resistance(ME,2,98);
	set_resistance(ME,3,98);
	set_resistance(ME,4,98);
	set_resistance(ME,5,98);
	set_resistance(ME,6,98);
	set_resistance(ME,7,98);
	set_char_unkillable(ME,1);
	
	place_particle_num(ME,18,12,4);		
	place_particle_num(ME,19,12,4);		
	place_particle_num(ME,20,12,4);		

	break;

beginstate DEAD_STATE;
	sf(7,7,1);
	sf(7,20,1);
	sf(2,3,4);
	award_party_xp(300,5);
	begin_talk_mode(27);	
break;

beginstate START_STATE; 
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		//if (dist_to_char(get_target()) <= 16)
			set_state(3);
		//	else set_foe_target(ME,-1);
		}
	
	if (get_foe_target(ME,8,0)) {
		last_abil = get_current_tick();
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		last_abil = get_current_tick();
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	if ((get_attitude(ME) >= 10) && (dist_to_pc() <= 40)) {
		last_abil = get_current_tick();
		set_foe_target(ME,pc_num());
		set_state(3);
		}

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);
	used_abil = 0;

	if ((mess1 == 0) && (mess2 == 0) && (get_health(ME) < get_max_health(ME))) {
		mess1 = 1;
		if (gf(100,7) == 0)
			begin_talk_mode(56);
		}
	if (gf(100,7) > 0)
		mess2 = 1;
	if ((mess2 == 0) && (tick_difference(last_abil,get_current_tick()) > 2)) {
		mess2 = 1;
		begin_talk_mode(57);
		}
	if ((mess3 == 0) && (gf(100,7) > 0) && (get_health(ME) < (get_max_health(ME) * 2) / 3)) {
		set_char_unkillable(ME,0);
		mess3 = 1;
		begin_talk_mode(90);	
		}
	
	if ((mess2 > 0) && (can_see_char(get_target())) && (is_combat()) && (tick_difference(last_abil,get_current_tick()) > 0)) {
		i = random_group_member(0);
		if (can_see_char(i) == FALSE)
			i = random_group_member(0);
		if (can_see_char(i) == FALSE)
			i = random_group_member(0);
		if (can_see_char(i)) {

			run_char_animation(2,1,35);	
	
			if (pc_num() == i) {
				print_named_str(ME,"summons a spray of poison.");
				set_char_status(i,2,5);
				pc_heard_sound_delay(177,100);						
				shoot_projectile(ME,get_target(),32);
				used_abil = 1;
				}
				else { // beam terror
					print_named_str(ME,"tries to control a creation's mind.");
					set_char_status(get_target(),7,25);
					pc_heard_sound_delay(170,100);						
					used_abil = 1;
					}
	
			last_abil = get_current_tick();
			end();
			}	
		}

	if (used_abil == 0)
		do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(5) == 0) {
		print_str("Talking: It doesn't respond.");
		}
		else begin_talk_mode(get_memory_cell(5));
break;